package com.live.proxy.home;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.live.proxy.R;
import com.live.proxy.model.ThaliModel;
import com.live.proxy.model.UserModel;
import com.live.proxy.sql.UserDatabase;
import com.live.proxy.tool.GetUtil;
import com.live.proxy.tool.OkHttpUtils;
import com.live.proxy.tool.StatusBarUtils;
import com.live.proxy.view.ToastUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

public class ThaliListActivity extends AppCompatActivity {
    ImageButton thali_back;
    RecyclerView thali_recyclerview;

    UserDatabase userDb;
    UserModel userModel;
    List<ThaliModel> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_thali_list);
        StatusBarUtils.setDarkStatusWhite(this,R.color.white);
        thali_back = findViewById(R.id.thali_back);
        thali_recyclerview = findViewById(R.id.thali_recyclerview);

        userDb = new UserDatabase(getApplicationContext());
        userModel = userDb.select();
        try {
            JSONObject appJson = GetUtil.getAppInfo(getApplicationContext());
            getAccount(GetUtil.getThaliIndustryList(),appJson.toString(),userModel.getToken());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private MyAdapter adapter;

    class MyAdapter extends RecyclerView.Adapter {
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MyAdapter.MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_thali, parent, false));
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            MyAdapter.MyViewHolder mholder = (MyAdapter.MyViewHolder) holder;
            mholder.item_thali_name.setText(list.get(position).getThaliName());
            mholder.item_thali_vip_amount.setText(String.valueOf(list.get(position).getVipAmount()));
            mholder.item_thali_qa_amount.setText(String.valueOf(list.get(position).getQaAmount()));
            mholder.item_thali_train_amount.setText(String.valueOf(list.get(position).getTrainAmount()));
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        class MyViewHolder extends RecyclerView.ViewHolder {
            TextView item_thali_name;
            TextView item_thali_vip_amount;
            TextView item_thali_qa_amount;
            TextView item_thali_train_amount;

            public MyViewHolder(View itemView) {
                super(itemView);
                item_thali_name = itemView.findViewById(R.id.item_thali_name);
                item_thali_vip_amount = itemView.findViewById(R.id.item_thali_vip_amount);
                item_thali_qa_amount = itemView.findViewById(R.id.item_thali_qa_amount);
                item_thali_train_amount = itemView.findViewById(R.id.item_thali_train_amount);
            }
        }
    }

    public static final int RRQUEST_FAILURE = 1;
    public static final int RRQUEST_SUCCESS = 2;
    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case RRQUEST_FAILURE:
                    ToastUtils.Setinfo(getApplicationContext(),msg.obj.toString(),2);
                    break;
                case RRQUEST_SUCCESS:
                    try {
                        JSONObject json = new JSONObject(msg.obj.toString());
                        if(json.getInt("code") == 1){
                            JSONObject valueJson = json.getJSONObject("data");
                            JSONArray thalisArray = valueJson.getJSONArray("thalis");
                            for (int i = 0 ; i < thalisArray.length() ;i++){
                                JSONObject thalisJson = thalisArray.getJSONObject(i);
                                ThaliModel model = new ThaliModel();
                                model.setThaliId(thalisJson.getInt("thali_id"));
                                model.setThaliName(thalisJson.getString("thali_name"));
                                model.setVipAmount(thalisJson.getInt("vip_amount"));
                                model.setQaAmount(thalisJson.getInt("qa_amount"));
                                model.setTrainAmount(thalisJson.getInt("train_amount"));
                                list.add(model);
                            }

                            adapter = new MyAdapter();
                            thali_recyclerview.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
                            thali_recyclerview.setAdapter(adapter);
                        }else{
                            ToastUtils.Setinfo(getApplicationContext(),json.getString("info"),2);
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    break;
            }
        }
    };
    public void getAccount(String url,String jsonStr,String token){
        OkHttpUtils.sendOkHttpJsonTokenRequest(url, jsonStr,new okhttp3.Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                Message message = new Message();
                message.what = RRQUEST_FAILURE;
                message.obj = "【请求异常】请联系管理员";
                handler.sendMessage(message);
            }
            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                String responseData = response.body().string();
                Message message = new Message();
                message.what = RRQUEST_SUCCESS;
                message.obj = responseData;
                handler.sendMessage(message);
            }
        },token);
    }
}